package com.timmy.google_play.holder.category;

import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.timmy.google_play.R;
import com.timmy.google_play.base.BaseHolder;
import com.timmy.google_play.bean.CategoryBean;
import com.timmy.google_play.conf.Constants;
import com.timmy.google_play.utils.UIUtils;

import butterknife.BindView;
import it.sephiroth.android.library.picasso.Picasso;

/**
 * Created by Timmy on 2016/12/13.
 * <p>
 * Category普通条目
 */
public class CategoryNormalHolder extends BaseHolder<CategoryBean> {
    @BindView(R.id.item_category_icon_1)
    ImageView    mItemCategoryIcon1;
    @BindView(R.id.item_category_name_1)
    TextView     mItemCategoryName1;
    @BindView(R.id.item_category_item_1)
    LinearLayout mItemCategoryItem1;
    @BindView(R.id.item_category_icon_2)
    ImageView    mItemCategoryIcon2;
    @BindView(R.id.item_category_name_2)
    TextView     mItemCategoryName2;
    @BindView(R.id.item_category_item_2)
    LinearLayout mItemCategoryItem2;
    @BindView(R.id.item_category_icon_3)
    ImageView    mItemCategoryIcon3;
    @BindView(R.id.item_category_name_3)
    TextView     mItemCategoryName3;
    @BindView(R.id.item_category_item_3)
    LinearLayout mItemCategoryItem3;

    @Override
    protected View initHolderVIew() {
        return View.inflate(UIUtils.getContext(), R.layout.item_category_normal, null);
    }

    @Override
    protected void refreshHolderView(CategoryBean data) {

        setData(data.name1, data.url1, mItemCategoryName1, mItemCategoryIcon1);
        setData(data.name2, data.url2, mItemCategoryName2, mItemCategoryIcon2);
        setData(data.name3, data.url3, mItemCategoryName3, mItemCategoryIcon3);

    }

    private void setData(final String name, String url, TextView tv, ImageView iv) {

        if (TextUtils.isEmpty(name) && TextUtils.isEmpty(url)) {

            //如果对应的数据是空的,则隐藏该view
            ViewParent parent = tv.getParent();

            ((ViewGroup)parent).setVisibility(View.INVISIBLE);

        }else{

            //显示
            ViewParent parent = tv.getParent();
            ((ViewGroup)parent).setVisibility(View.VISIBLE);

            //设置数据
            tv.setText(name);

            //图标
            Picasso.with(UIUtils.getContext()).load(Constants.URLS.IMAGEBASEURL + url).into(iv);

            //设置点击时间
            ((ViewGroup)parent).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(UIUtils.getContext(), name, Toast.LENGTH_SHORT).show();
                }
            });
        }
    }
}
